<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="名称">
              <a-input placeholder="请输入名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="设备类型">
              <j-dict-select-tag placeholder="请选择设备类型" v-model="queryParam.did" dictCode="device_type"/>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="所属项目">
                <j-dict-select-tag placeholder="请选择所属项目" v-model="queryParam.projectId" dictCode="tb_project,project_name,id"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="所属分组">
                <j-dict-select-tag placeholder="请选择所属分组" v-model="queryParam.groupId" dictCode="tb_group,group_name,id"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="在线状态">
                <j-switch placeholder="请选择在线状态" v-model="queryParam.olStatus"  query></j-switch>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="灯具类型">
                <j-dict-select-tag placeholder="请选择灯具类型" v-model="queryParam.lampType" dictCode="lamp_type"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
                <a-button @click="handleAdd" type="primary" icon="plus"  style="margin-left: 8px">新增</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END --> 
    <!-- table区域-begin -->
    <div>  
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="setBrightness(record)">设置亮度</a>
          <a-divider type="vertical" />
          <a @click="watchHistory(record)">照明数据</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
        <span slot="onStatus" slot-scope="text,record">
          <!-- <a-switch v-model:checked="record.onStatus" @click="toggle(record)" /> -->
          <j-switch v-model="record.onStatus"  :options="['1','0']" @change="toggle(record)"></j-switch>
        </span>
      </a-table>
    </div>

    <lamp-modal ref="modalForm" @ok="modalFormOk"></lamp-modal>
    <brightness-modal ref="brightnessModal"  @ok="refreshData"></brightness-modal>
    <history-modal ref="historyModal"></history-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import LampModal from './modules/LampModal'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import BrightnessModal from './modules/brightnessModal'
  import HistoryModal from './modules/HistoryModal'
  import { httpAction, getAction } from '@/api/manage'

  export default {
    name: 'LampList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      LampModal,
      BrightnessModal,
      HistoryModal
    },
    data () {
      return {
        description: '照明管理管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'名称',
            align:"center",
            dataIndex: 'name'
          },
          {
            title:'灯控器地址',
            align:"center",
            dataIndex: 'code'
          },
          {
            title:'端口号',
            align:"center",
            dataIndex: 'ep'
          },
          {
            title:'设备类型',
            align:"center",
            dataIndex: 'did_dictText'
          },
          {
            title:'运行时间（分）',
            align:"center",
            dataIndex: 'lightrt'
          },
          {
            title:'当前亮度',
            align:"center",
            dataIndex: 'bri'
          },
          {
            title:'所属项目',
            align:"center",
            dataIndex: 'projectId_dictText'
          },
          {
            title:'所属分组',
            align:"center",
            dataIndex: 'groupId_dictText'
          },
          {
            title:'在线状态',
            align:"center",
            dataIndex: 'olStatus_dictText'
          },
          {
            title:'开关状态',
            align:"center",
            dataIndex: 'onStatus',
            scopedSlots: { customRender: 'onStatus' }
          },
          {
            title:'灯具类型',
            align:"center",
            dataIndex: 'lampType_dictText'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/corp/lamp/list",
          delete: "/corp/lamp/delete",
          deleteBatch: "/corp/lamp/deleteBatch",
          exportXlsUrl: "/corp/lamp/exportXls",
          importExcelUrl: "corp/lamp/importExcel",
          control: "/corp/lamp/control",
        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
      this.$set(this.dictOptions, 'olStatus', [{text:'是',value:'Y'},{text:'否',value:'N'}])
      this.$set(this.dictOptions, 'onStatus', [{text:'是',value:'Y'},{text:'否',value:'N'}])
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      refreshData(){
        setTimeout(()=>{
          this.modalFormOk()
        },2000)
      },
      toggle(record){
        let that = this
        var params = {id:record.id,on:record.onStatus=='1' ? 1 : 0}
        that.confirmLoading = true;
        let httpurl = this.url.control+"?id="+params.id+"&on="+params.on;
        getAction(httpurl,{}).then((res)=>{
          if(res.success){
            that.$message.success("操作成功");
          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.confirmLoading = false;
        })
      },
      // 查看历史数据
      watchHistory(item){
        this.$refs.historyModal.open(item)
        this.$refs.historyModal.title = item.name
      },
      setBrightness(record){
        var params = {id:record.id,bri:record.bri,name:record.name}
        this.$refs.brightnessModal.show(params);
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'name',text:'名称',dictCode:''})
        fieldList.push({type:'string',value:'code',text:'灯控器地址',dictCode:''})
        fieldList.push({type:'int',value:'ep',text:'端口号',dictCode:''})
        fieldList.push({type:'int',value:'pid',text:'协议',dictCode:''})
        fieldList.push({type:'int',value:'did',text:'设备类型 id',dictCode:'device_type'})
        fieldList.push({type:'int',value:'bri',text:'当前亮度状态: 1-254',dictCode:''})
        fieldList.push({type:'int',value:'colorTemp',text:'色温: 0-100',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'volt',text:'实时电压',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'curr',text:'实时电流',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'actp',text:'有功功率',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'acte',text:'有功电能',dictCode:''})
        fieldList.push({type:'Text',value:'lightrt',text:'运行时间（分）',dictCode:''})
        fieldList.push({type:'string',value:'gwlongitude',text:'经度',dictCode:''})
        fieldList.push({type:'string',value:'gwlatitude',text:'纬度',dictCode:''})
        fieldList.push({type:'int',value:'rssi',text:'设备信号强度：0-100',dictCode:''})
        fieldList.push({type:'string',value:'imsi',text:'设备SIM卡标识号',dictCode:''})
        fieldList.push({type:'string',value:'projectId',text:'所属项目id',dictCode:"tb_project,project_name,id"})
        fieldList.push({type:'string',value:'groupId',text:'所属分组id',dictCode:"tb_group,group_name,id"})
        fieldList.push({type:'string',value:'pci',text:'所属基站',dictCode:''})
        fieldList.push({type:'switch',value:'olStatus',text:'在线状态:0.掉线; 1.在线'})
        fieldList.push({type:'switch',value:'onStatus',text:'开关状态：0.关; 1 开'})
        fieldList.push({type:'string',value:'status',text:'设备状态: 0.禁用; 1. 启用; 2.报警',dictCode:'is_open'})
        fieldList.push({type:'string',value:'lampType',text:'灯具类型',dictCode:'lamp_type'})
        fieldList.push({type:'string',value:'hver',text:'硬件版本号',dictCode:''})
        fieldList.push({type:'string',value:'sver',text:'软件版本号',dictCode:''})
        fieldList.push({type:'string',value:'mac',text:'所属云盒mac地址',dictCode:''})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>